﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using Dapper;
using MODEL;
using System.Threading;

namespace DAL
{
    public class AlertMakeInfoDAL
    {
        public static async Task<IEnumerable<AlertMakeInfo>> GetAlertMakeInfosAsync(string userId = null)
        {
            string sql = @"SELECT A.ID
                        , A.month
                        ,A.minVal
                        ,A.maxVal
                        ,A.name
                        ,A.addMan
                        ,A.createTime
                        ,S.stationId
                        ,S.stationCode
                        ,S.stationType
                        ,S.stationName 
                        ,S.elementList
                        ,E.elementCode
                        ,E.Id
                        ,E.elementName
                        FROM msap_db.alert_make_infor A
                        INNER JOIN msap_db.station_infor S
                        ON A.stationId = S.stationId
                        INNER JOIN msap_db.element_infor E
                        ON S.stationType = E.stationType
                        AND A.elementCode = E.elementCode
                        AND (@UserId IS NULL 
	                        OR EXISTS(
		                        SELECT 1 FROM msap_db.station_user SU 
                                WHERE 
                                SU.stationId = S.stationId 
                                AND SU.userId = @UserId))
                        ORDER BY A.createTime DESC";

            using (MySqlConnection conn = new MySqlConnection(AppSetting.Instance.MySqlConnectionStr))
            {
                return await conn.QueryAsync<AlertMakeInfo, StationInfo, ElementInfo, AlertMakeInfo>(sql, (alertMakeInfo, stationInfo, elementInfo) =>
                {
                    alertMakeInfo.StationInfo = stationInfo;
                    alertMakeInfo.ElementInfo = elementInfo;
                    return alertMakeInfo;
                },
                new { UserId = userId },
                splitOn: "stationId,elementCode");
            }
        }

        public static async Task<int> InsertOrUpdateAlertMakeInfo(AlertMakeInfo alertMakeInfo)
        {
            string sql = @"INSERT INTO msap_db.alert_make_infor(
                              Id
                            , stationId
                            , elementCode
                            , month
                            , maxVal
                            , minVal
                            , name
                            , addMan
                            , createTime)
                            VALUES(@Id, @StationId,@ElementCode,@Month,@MaxVal,@MinVal,@Name,@AddMan,@CreateTime)
                            ON DUPLICATE KEY UPDATE                            
                            stationId=VALUES(stationId)
                            , elementCode=VALUES(elementCode)
                            ,month=VALUES(month)
                            ,maxVal=VALUES(maxVal)
                            ,minVal=VALUES(minVal)
                            ,name=VALUES(name)
                            ,addMan=VALUES(addMan)
                            ,createTime=VALUES(createTime)";

            return await Task.Run(() =>
            {
                Thread.Sleep(10000);
                return 1;
            });

            //using (MySqlConnection conn = new MySqlConnection(AppSetting.Instance.MySqlConnectionStr))
            //{
            //    return await conn.ExecuteAsync(sql, param: new
            //    {
            //        Id = string.IsNullOrEmpty(alertMakeInfo.Id) ? Guid.NewGuid().ToString() : alertMakeInfo.Id,
            //        StationId = alertMakeInfo.StationInfo.StationId,
            //        ElementCode = alertMakeInfo.ElementInfo.ElementCode,
            //        Month = alertMakeInfo.Month,
            //        MaxVal = alertMakeInfo.MaxVal,
            //        MinVal = alertMakeInfo.MinVal,
            //        Name = alertMakeInfo.Name,
            //        AddMan = alertMakeInfo.AddMan,
            //        CreateTime = alertMakeInfo.CreateTime
            //    });
            //}
        }

        public static async Task<int> DeleteAlertMakeInfo(List<string> idList)
        {
            string sql = $"DELETE FROM msap_db.alert_make_infor WHERE Id IN ({string.Join(",", idList.Select(id=>$"'{id}'"))}) LIMIT {idList.Count.ToString()}";

            using (MySqlConnection conn = new MySqlConnection(AppSetting.Instance.MySqlConnectionStr))
            {
                return await conn.ExecuteAsync(sql);
            }
        }
    }
}
